<div class="container-xl">
  <!-- Page title -->
  <div class="page-header d-print-none">
    <div class="row align-items-center">
      <div class="col">
        <h2 class="page-title">
          自定义订阅
        </h2>
      </div>
      <div class="col-auto ms-auto d-print-none">
        <div class="btn-list">
          <a href="javascript:show_userrss_modal()" class="btn btn-primary d-none d-sm-inline-block">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
              stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none" />
              <line x1="12" y1="5" x2="12" y2="19" />
              <line x1="5" y1="12" x2="19" y2="12" />
            </svg>
            新建订阅任务
          </a>
          <a href="javascript:show_userrss_modal()" class="btn btn-primary d-sm-none btn-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
              stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none" />
              <line x1="12" y1="5" x2="12" y2="19" />
              <line x1="5" y1="12" x2="19" y2="12" />
            </svg>
          </a>
          <a href="javascript:navmenu('rss_parser')" class="btn d-none d-sm-inline-block">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-cpu" width="24" height="24"
              viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
              stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
              <rect x="5" y="5" width="14" height="14" rx="1"></rect>
              <path d="M9 9h6v6h-6z"></path>
              <path d="M3 10h2"></path>
              <path d="M3 14h2"></path>
              <path d="M10 3v2"></path>
              <path d="M14 3v2"></path>
              <path d="M21 10h-2"></path>
              <path d="M21 14h-2"></path>
              <path d="M14 21v-2"></path>
              <path d="M10 21v-2"></path>
            </svg>
            RSS解析器
          </a>
          <a href="javascript:navmenu('rss_parser')" class="btn d-sm-none btn-icon" title="RSS解析器">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-cpu" width="24" height="24"
              viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
              stroke-linejoin="round">
              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
              <rect x="5" y="5" width="14" height="14" rx="1"></rect>
              <path d="M9 9h6v6h-6z"></path>
              <path d="M3 10h2"></path>
              <path d="M3 14h2"></path>
              <path d="M10 3v2"></path>
              <path d="M14 3v2"></path>
              <path d="M21 10h-2"></path>
              <path d="M21 14h-2"></path>
              <path d="M14 21v-2"></path>
              <path d="M10 21v-2"></path>
            </svg>
          </a>
        </div>
      </div>
    </div>
  </div>
</div>
{% if Count > 0 %}
<div class="page-body">
  <div class="container-xl">
    <div class="row row-cards">
      {% for Task in Tasks %}
      <div class="card">
        <div class="card-header">
          {% if Task.state == 'Y' %}
          <span class="badge bg-green"></span>
          {% else %}
          <span class="badge bg-red"></span>
          {% endif %}
          <div class="ms-2"><h3 class="card-title">{{ Task.name }}</h3></div>
          <div class="ms-2">
            <a href="javascript:run_userrss_now('{{ Task.id }}')" class="btn-icon" title="立即运行任务"
              data-bs-toggle="tooltip">
              <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-bolt icon-filled" width="24"
                height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none"
                stroke-linecap="round" stroke-linejoin="round">
                <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                <polyline points="13 3 13 10 19 10 11 21 11 14 5 14 13 3"></polyline>
              </svg>
            </a>
          </div>
          <div class="card-actions btn-actions">
            <a href="javascript:show_userrss_detail('{{ Task.id }}')" class="btn-action">
              <svg xmlns="http://www.w3.org/2000/svg" class="icon ms-1" width="20" height="20" fill="currentColor"
                viewBox="0 0 16 16">
                <path fill-rule="evenodd"
                  d="M2.5 12a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5zm0-4a.5.5 0 0 1 .5-.5h10a.5.5 0 0 1 0 1H3a.5.5 0 0 1-.5-.5z" />
              </svg>
            </a>
            {% if Task.uses == 'D' %}
            <a href="javascript:show_rss_articles_modal('{{ Task.id }}')" class="btn-action">
              <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-eye" width="24" height="24"
                viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
                stroke-linejoin="round">
                <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                <circle cx="12" cy="12" r="2"></circle>
                <path d="M22 12c-2.667 4.667 -6 7 -10 7s-7.333 -2.333 -10 -7c2.667 -4.667 6 -7 10 -7s7.333 2.333 10 7">
                </path>
              </svg>
            </a>
            {% endif %}
            <a href="javascript:edit_userrss_modal('{{ Task.id }}')" class="btn-action" title="编辑任务">
              <svg xmlns="http://www.w3.org/2000/svg" class="icon ms-1" width="24" height="24" viewBox="0 0 24 24"
                stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                <path d="M9 7h-3a2 2 0 0 0 -2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2 -2v-3" />
                <path d="M9 15h3l8.5 -8.5a1.5 1.5 0 0 0 -3 -3l-8.5 8.5v3" />
                <line x1="16" y1="5" x2="19" y2="8" />
              </svg>
            </a>
            <a href="javascript:del_userrss_modal('{{ Task.id }}', '{{ Task.name }}')" class="btn-action" title="删除任务">
              <svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="24" viewBox="0 0 24 24"
                stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                <desc>Download more icon variants from https://tabler-icons.io/i/x</desc>
                <path stroke="none" d="M0 0h24v24H0z" fill="none" />
                <line x1="18" y1="6" x2="6" y2="18" />
                <line x1="6" y1="6" x2="18" y2="18" />
              </svg>
            </a>
          </div>
        </div>
        <div class="card-body" id="detail_{{ Task.id }}"
          style="display: {% if Count > 2 %}none{% else %}block{% endif %};">
          <div class="datagrid">
            <div class="datagrid-item">
              <div class="datagrid-title">地址</div>
              <div class="datagrid-content">
                <div class="d-flex align-items-center" style="word-break: break-all;">
                  {{ Task.address|split('?', 0) }}
                </div>
              </div>
            </div>
            <div class="datagrid-item">
              <div class="datagrid-title">解析器</div>
              <div class="datagrid-content">
                <span class="badge me-2">{{ Task.parser_name }}</span>
              </div>
            </div>
            <div class="datagrid-item">
              <div class="datagrid-title">刷新间隔</div>
              <div class="datagrid-content">
                {{ Task.interval }} 分钟
              </div>
            </div>
            <div class="datagrid-item">
              <div class="datagrid-title">动作</div>
              <div class="datagrid-content">
                <span class="badge me-2 bg-blue">{{ Task.uses_text }}</span>
              </div>
            </div>
            <div class="datagrid-item" style="display: {% if Task.uses != 'D' %}none{% else %}block{% endif %};">
              <div class="datagrid-title">包含</div>
              <div class="datagrid-content">
                {% if Task.include %}
                <span class="badge badge-outline text-green me-2 text-wrap text-start">{{ Task.include }}</span>
                {% endif %}
              </div>
            </div>
            <div class="datagrid-item" style="display: {% if Task.uses != 'D' %}none{% else %}block{% endif %};">
              <div class="datagrid-title">排除</div>
              <div class="datagrid-content">
                {% if Task.exclude %}
                <span class="badge badge-outline text-red me-2 text-wrap text-start">{{ Task.exclude }}</span>
                {% endif %}
              </div>
            </div>
            <div class="datagrid-item" style="display: {% if Task.uses != 'D' %}none{% else %}block{% endif %};">
              <div class="datagrid-title">过滤规则</div>
              <div class="datagrid-content">
                {% if Task.filter_name %}
                <span class="badge badge-outline text-orange me-2">{{ Task.filter_name }}</span>
                {% endif %}
              </div>
            </div>
            <div class="datagrid-item" style="display: {% if Task.uses != 'D' %}none{% else %}block{% endif %};">
              <div class="datagrid-title">保存路径</div>
              <div class="datagrid-content">{{ Task.save_path or '自动' }}</div>
            </div>
            <div class="datagrid-item">
              <div class="datagrid-title">状态</div>
              <div class="datagrid-content">
                {% if Task.state == 'Y' %}
                <span class="badge me-2 bg-green">正在运行</span>
                {% else %}
                <span class="badge me-2 bg-red">已停用</span>
                {% endif %}
              </div>
            </div>
            <div class="datagrid-item">
              <div class="datagrid-title">已处理数量</div>
              <div class="datagrid-content">
                {% if Task.uses == 'D' %}
                <a id="rss_history_btn" href="javascript:show_rss_history_modal('{{ Task.id }}')" class="btn-link">
                  {{ Task.counter or 0 }}
                </a>
                {% else %}
                {{ Task.counter or 0 }}
                {% endif %}
              </div>
            </div>
            <div class="datagrid-item">
              <div class="datagrid-title">最后更新时间</div>
              <div class="datagrid-content">
                {{ Task.update_time or '' }}
              </div>
            </div>
            {% if Task.uses == 'D' %}
            <div class="datagrid-item">
              <div class="datagrid-title">下载设置</div>
              <div class="datagrid-content">
                {% if Task.download_setting|string in DownloadSettings %}
                <span class="badge me-2 bg-yellow">{{ DownloadSettings[Task.download_setting|string].name }}</span>
                {% else %}
                <span class="badge me-2 bg-red">{{ DownloadSettings["-1"].name }}</span>
                {% endif %}
              </div>
            </div>
            {% endif %}
          </div>
        </div>
      </div>
      {% endfor %}
    </div>
  </div>
</div>
{% else %}
<div class="page-body">
  <div class="container-xl d-flex flex-column justify-content-center">
    <div class="empty">
      <div class="empty-img"><img src="./static/img/quitting_time.svg" height="128" alt="">
      </div>
      <p class="empty-title">没有记录</p>
      <p class="empty-subtitle text-muted">
        当前没有正在运行的自定义订阅任务
      </p>
    </div>
  </div>
</div>
{% endif %}
<div class="modal modal-blur fade" id="modal-userrss" tabindex="-1" role="dialog" aria-hidden="true"
  data-bs-backdrop="static" data-bs-keyboard="false">
  <div class="modal-dialog modal-lg modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="userrss_modal_title"></h5>
        <input type="hidden" id="userrss_id">
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        <div class="row">
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label required">名称</label>
              <input type="text" id="userrss_name" class="form-control" placeholder="别名">
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label required">状态</label>
              <select class="form-select" id="userrss_state">
                <option value="Y" selected>正常</option>
                <option value="N">停用</option>
              </select>
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label required">刷新间隔(分钟) <span class="form-help" title="检查RSS更新的间隔时间"
                  data-bs-toggle="tooltip">?</span></label>
              <input type="text" id="userrss_interval" class="form-control" placeholder="30">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-lg-8">
            <div class="mb-3">
              <label class="form-label required">地址 <span class="form-help" title="RSS订阅的链接地址"
                  data-bs-toggle="tooltip">?</span></label>
              <input type="text" id="userrss_address" class="form-control" placeholder="RSS地址">
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label required">解析器 <span class="form-help" title="使用RSS解析器中配置的解析格式解析RSS报文"
                  data-bs-toggle="tooltip">?</span></label>
              <select class="form-select" id="userrss_parser">
                {% for RssParser in RssParsers %}
                <option value="{{ RssParser.id }}" {% if loop.first %}selected{% endif %}>{{ RssParser.name }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
        </div>
        <div class="form-selectgroup-boxes row mb-3">
          <label class="form-label required">动作</label>
          <div class="col-lg-4">
            <div class="mb-1">
              <label class="form-selectgroup-item" for="use_D">
                <input type="radio" name="userrss_uses" id="use_D" value="D" class="form-selectgroup-input" checked>
                <span class="form-selectgroup-label d-flex align-items-center p-3">
                  <span class="me-3">
                    <span class="form-selectgroup-check"></span>
                  </span>
                  <span class="form-selectgroup-label-content">
                    <span class="form-selectgroup-title strong mb-1">下载</span>
                  </span>
                </span>
              </label>
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-1">
              <label class="form-selectgroup-item" for="use_R">
                <input type="radio" name="userrss_uses" id="use_R" value="R" class="form-selectgroup-input">
                <span class="form-selectgroup-label d-flex align-items-center p-3">
                  <span class="me-3">
                    <span class="form-selectgroup-check"></span>
                  </span>
                  <span class="form-selectgroup-label-content">
                    <span class="form-selectgroup-title strong mb-1">订阅</span>
                  </span>
                </span>
              </label>
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-1">
              <label class="form-selectgroup-item" for="use_S">
                <input type="radio" name="userrss_uses" id="use_S" value="S" class="form-selectgroup-input">
                <span class="form-selectgroup-label d-flex align-items-center p-3">
                  <span class="me-3">
                    <span class="form-selectgroup-check"></span>
                  </span>
                  <span class="form-selectgroup-label-content">
                    <span class="form-selectgroup-title strong mb-1">搜索</span>
                  </span>
                </span>
              </label>
            </div>
          </div>
        </div>
        <div class="row" id="filter_div">
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label">包含 <span class="form-help" title="RSS报文中title符合包括规则的才会被处理"
                  data-bs-toggle="tooltip">?</span></label>
              <input type="text" id="userrss_include" class="form-control" placeholder="关键字/正则表达式">
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label">排除 <span class="form-help" title="RSS报文中title符合排除规则的则不会被处理"
                  data-bs-toggle="tooltip">?</span></label>
              <input type="text" id="userrss_exclude" class="form-control" placeholder="关键字/正则表达式">
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label">过滤规则 <span class="form-help" title="只有符合过滤规则的才会被处理"
                  data-bs-toggle="tooltip">?</span></label>
              <select class="form-select" id="userrss_filterrule">
                <option value="" selected>请选择</option>
                {% for FilterRule in FilterRules %}
                <option value="{{ FilterRule.id }}">{{ FilterRule.name }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
        </div>
        <div class="row" id="downloader_setting_div">
          <div class="col-lg-8">
            <div class="mb-3">
              <label class="form-label">保存路径</label>
              <input type="text" id="userrss_save_path" class="form-control" placeholder="留空自动选择保存路径">
            </div>
          </div>
          <div class="col-lg-4">
            <div class="mb-3">
              <label class="form-label">下载设置</label>
              <select class="form-select" id="userrss_download_setting">
                {% for Id, Attr in DownloadSettings.items() %}
                <option value="{{ Attr.id }}">{{ Attr.name }}</option>
                {% endfor %}
              </select>
            </div>
          </div>
        </div>
        {% if Count > 0 %}
        <details>
          <summary class="summary">
            模板
          </summary>
          <div class="row mt-2">
            <div class="form-selectgroup">
              {% for Task in Tasks %}
              <label class="form-selectgroup-item">
                <input type="button" class="form-selectgroup-input" onclick="apply_templated('{{ Task.id }}')">
                <span class="form-selectgroup-label">{{ Task.name }}</span>
              </label>
              {% endfor %}
            </div>
          </div>
        </details>
        {% endif %}
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-link me-auto" data-bs-dismiss="modal">取消</button>
        <a href="javascript:add_or_edit_userrss_task()" id="add_or_edit_userrss_btn" class="btn btn-primary">保存</a>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-rss-articles" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-xl modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <div class="ms-2">
          <h5 class="modal-title">订阅预览</h5>
          <input type="hidden" id="article_check_id" value="" >
        </div>
        <div class="ms-5">
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
      </div>      
      <div class="table-responsive" style="overflow-y: auto; max-height: 35em;">
        <table id="table-rss-articles" class="table table-vcenter card-table">
        </table>
      </div>
      <div class="modal-footer">
        <a href="javascript:batch_articles_action('download')"
          class="btn btn-primary me-auto">
          <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-download" width="24" height="24"
            viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
            stroke-linejoin="round">
            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
            <path d="M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-2"></path>
            <polyline points="7 11 12 16 17 11"></polyline>
            <line x1="12" y1="4" x2="12" y2="16"></line>
          </svg>
          批量下载
        </a>
        <a href="javascript:batch_articles_action('set_finished')"
          class="btn btn-green">
          <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-circle-check" width="24"
            height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
            stroke-linejoin="round">
            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
            <circle cx="12" cy="12" r="9"></circle>
            <path d="M9 12l2 2l4 -4"></path>
          </svg>
          已处理</a>
        <a href="javascript:batch_articles_action('set_unfinish')"
          class="btn btn-yellow">
          <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-circle-x" width="24" height="24"
            viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round"
            stroke-linejoin="round">
            <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
            <circle cx="12" cy="12" r="9"></circle>
            <path d="M10 10l4 4m0 -4l-4 4"></path>
          </svg>
          未处理
        </a>
      </div>
    </div>
  </div>
</div>
<div class="modal modal-blur fade" id="modal-rss-history" tabindex="-1" role="dialog" aria-hidden="true">
  <div class="modal-dialog modal-xl modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">订阅下载历史</h5>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="table-responsive" style="overflow-y: auto; max-height: 35em;">
        <table id="table-rss-history" class="table table-vcenter card-table">
        </table>
      </div>
    </div>
  </div>
</div>
<script type="text/javascript">
  // 显示新增任务
  function show_userrss_modal() {
    $("#userrss_id").val('');
    $("#userrss_modal_title").text("新增订阅");
    $("#userrss_name").val('');
    $("#userrss_interval").val('');
    $("#userrss_address").val('');
    $("#userrss_include").val('');
    $("#userrss_exclude").val('');
    $("#userrss_filterrule").val('');
    $("#userrss_parser").val('1');
    $("#userrss_state").val('N');
    show_userrss_uses('D');
    $("#userrss_save_path").val('');
    $("#userrss_download_setting").val('-1');
    $("#modal-userrss").modal('show');
  }

  // 显示编辑任务
  function edit_userrss_modal(id) {
    $("#userrss_id").val(id);
    show_wait_process();
    ajax_post("get_userrss_task", { id: id }, function (ret) {
      hide_wait_process();
      if (ret.code == 0) {
        $("#userrss_modal_title").text("编辑订阅");
        $("#userrss_name").val(ret.detail.name);
        $("#userrss_interval").val(ret.detail.interval);
        $("#userrss_address").val(ret.detail.address);
        $("#userrss_include").val(ret.detail.include);
        $("#userrss_exclude").val(ret.detail.exclude);
        $("#userrss_filterrule").val(ret.detail.filter);
        $("#userrss_parser").val(ret.detail.parser);
        $("#userrss_state").val(ret.detail.state);
        show_userrss_uses(ret.detail.uses);
        $("#userrss_save_path").val(ret.detail.save_path);
        $("#userrss_download_setting").val(ret.detail.download_setting);
        $("#modal-userrss").modal('show');
      }
    });
  }

  // 显示删除任务
  function del_userrss_modal(taskid, name) {
    show_confirm_modal("删除订阅任务 " + name + " ？", function () {
      hide_confirm_modal();
      ajax_post("delete_userrss_task", { "id": taskid }, function (ret) {
        navmenu('user_rss');
      });
    });
  }

  // 折叠任务详情
  function show_userrss_detail(taskid) {
    if ($(`#detail_${taskid}`)[0].style["display"] == "none") {
      $(`#detail_${taskid}`).attr("style", "display: block;");
    } else {
      $(`#detail_${taskid}`).attr("style", "display: none;");
    };
  }

  // 新增/编辑任务保存
  function add_or_edit_userrss_task() {
    id = $("#userrss_id").val();
    name = $("#userrss_name").val();
    if (!name) {
      $("#userrss_name").addClass("is-invalid");
      return;
    } else {
      $("#userrss_name").removeClass("is-invalid");
    }
    interval = $("#userrss_interval").val();
    if (!interval || isNaN(interval)) {
      $("#userrss_interval").addClass("is-invalid");
      return;
    } else {
      $("#userrss_interval").removeClass("is-invalid");
    }
    address = $("#userrss_address").val();
    if (!address) {
      $("#userrss_address").addClass("is-invalid");
      return;
    } else {
      $("#userrss_address").removeClass("is-invalid");
    }
    parser = $("#userrss_parser").val();
    if (!parser) {
      $("#userrss_parser").addClass("is-invalid");
      return;
    } else {
      $("#userrss_parser").removeClass("is-invalid");
    }
    var params = {
      id: id,
      name: name,
      address: address,
      parser: parser,
      interval: interval,
      uses: $('input:radio[name=userrss_uses]:checked').val(),
      include: $("#userrss_include").val(),
      exclude: $("#userrss_exclude").val(),
      filterrule: $("#userrss_filterrule").val(),
      state: $("#userrss_state").val(),
      save_path: $("#userrss_save_path").val(),
      download_setting: $("#userrss_download_setting").val(),
    };
    $("#add_or_edit_userrss_btn").text("保存中").attr("disabled", true);
    ajax_post("update_userrss_task", params, function (ret) {
      $("#modal-userrss").modal('hide');
      $("#add_or_edit_userrss_btn").attr("disabled", false);
      navmenu('user_rss');
    });
  }

  // 立即运行任务
  function run_userrss_now(id) {
    show_wait_process();
    ajax_post("run_userrss", { "id": id }, function (ret) {
      hide_wait_process();
      show_success_modal("任务运行完成！", function () {
        navmenu('user_rss');
      });
    });
  }

  // 报文预览
  function show_rss_articles_modal(id) {
    show_wait_process();
    ajax_post("list_rss_articles", { "id": id }, function (ret) {
      hide_wait_process();
      $("#table-rss-articles").empty();
      if (ret.code == 0) {
        articles = ret.data;
        var content_th = `<thead><tr>
                          <th class="w-1"><input class="form-check-input m-0 align-middle" id="articles_check_all_btn" type="checkbox" aria-label="全选"></th>
                          <th>标题</th>
                          <th>状态</th>
                          <th>发布时间</th>
                          <th></th>
                          </tr></thead>`;
        var content_td = '';
        for (var i = 0; i < articles.length; i++) {
          var article = articles[i];
          var title = '';
          var link = '';
          var size = '';
          var date = '';
          var finish_flag = '';
          if (article.title) {
            title_param = article.title.replace(/\'/g, "&#39;");
            input_checkbox = `<input class="form-check-input m-0 align-middle" id="check_${i}" type="checkbox">
                              <input type="hidden" id="enclosure_${i}" value="${article.enclosure}">
                              <input type="hidden" id="title_${i}" value="${title_param}">`;
            title = `<span>${article.title}</span> 
                      <a class="ms-2" title="识别匹配" href='javascript:rss_article_test("${i}", "${id}", "${title_param}")' data-bs-toggle="tooltip">
                        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-text-recognition" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                           <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                           <path d="M4 8v-2a2 2 0 0 1 2 -2h2"></path>
                           <path d="M4 16v2a2 2 0 0 0 2 2h2"></path>
                           <path d="M16 4h2a2 2 0 0 1 2 2v2"></path>
                           <path d="M16 20h2a2 2 0 0 0 2 -2v-2"></path>
                           <path d="M12 16v-7"></path>
                           <path d="M9 9h6"></path>
                        </svg>
                      </a>`;
            if (article.link) {
              title = `<a href="${article.link}" target="_blank">${title}</a>`;
            }
          }
          if (article.date) {
            date = article.date.split(" ");
            date = `<small>${date[0]}<br>${date[1]}</small>`;
          }
          if (article.size) {
            size = `<span class="badge badge-outline text-green me-1 mb-1" title="大小">${article.size}</span>`;
          }
          if (article.finish_flag) {
            finish_flag = `<span class="badge badge-outline text-blue me-1 mb-1">已处理</span>`;
          } else {
            finish_flag = `<span class="badge badge-outline text-yellow me-1 mb-1">未处理</span>`;
          }
          download_btn = `<a href='javascript:single_article_download("${id}", "${i}")' title="下载">
                            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-download" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                              <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                              <path d="M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2 -2v-2"></path>
                              <polyline points="7 11 12 16 17 11"></polyline>
                              <line x1="12" y1="4" x2="12" y2="16"></line>
                            </svg>
                          </a>`
          var content_td = `${content_td}<tr><td>${input_checkbox}</td><td>${title}${size}<div id='info_${i}'></div></td>><td>${finish_flag}<a id='flag_${i}'></a></td><td class="text-nowrap">${date}</td><td>${download_btn}</td></tr>`;
          content_tb = `<tbody>${content_td}</tbody>`;
          content = `${content_th}${content_tb}`;
        }

        $("#table-rss-articles").append(content);
        // 绑定全选事件
        $("#articles_check_all_btn").bind("change", function () {
          $(".table-responsive input[type=checkbox]").prop("checked", $("#articles_check_all_btn").prop("checked"));
        });
      } else {
        var content = `<div class="empty"><p class="empty-title">${ret.msg}</p></div>`;
        $("#table-rss-articles").append(content);
      };
      $("#article_check_id").val(id);
      $("#modal-rss-articles").modal('show');
    });
  }

  // 获取选中按钮
  function get_article_checked(){
    var articles = [];
    $(".table-responsive input[type=checkbox]").each(function () {
      if ($(this).prop("checked")) {
        checkid = $(this).attr("id");
        if (checkid && checkid.startsWith("check_")) {
          title = $(`#${checkid.replace("check", "title")}`).val();
          enclosure = $(`#${checkid.replace("check", "enclosure")}`).val();
          if (title || enclosure) {
            articles.push({ title: title, enclosure: enclosure })
          }
        }
      }
    });
    return articles;
  }


  // 批量处理
  function batch_articles_action(flag) {
    var articles = get_article_checked();
    var id = $("#article_check_id").val();
    if (articles.length == 0) {
      return;
    }
    if (flag.startsWith("set_")) {
      articles_check(id, flag, articles);
    } else if (flag == "download") {
      articles_download(id, articles);
    }
  }


  // 报文处理设置
  function articles_check(id, flag, articles) {
    $("#modal-rss-articles").modal('hide');
    show_wait_process();
    ajax_post("rss_articles_check", { "articles": articles, "flag": flag }, function (ret) {
      hide_wait_process();
      show_rss_articles_modal(id);
    });
  }

  // 报文下载
  function articles_download(id, articles) {
    $("#modal-rss-articles").modal('hide');
    show_wait_process();
    ajax_post("rss_articles_download", { "articles": articles, "taskid": id }, function (ret) {
      hide_wait_process();
      if (ret.code == 0) {
        show_success_modal("添加下载成功！", function() {
          show_rss_articles_modal(id);//
        });
      } else if (ret.code == 1) {
        show_fail_modal(" 添加下载失败！", function() {
          show_rss_articles_modal(id);//
        });
      }
    });
  }

  // 单个报文下载
  function single_article_download(id, article_id) {
    article = [{ title: $(`#title_${article_id}`).val(), enclosure: $(`#enclosure_${article_id}`).val() }];
    articles_download(id, article);
  }

  // rss报文测试
  function rss_article_test(id, taskid, title) {
    var info_id = `#info_${id}`;
    var flag_id = `#flag_${id}`;
    make_cursor_busy();
    ajax_post("rss_article_test", { "taskid": taskid, "title": title }, function (ret) {
      cancel_cursor_busy();
      if (ret.code == 0) {
        $(info_id).empty();
        $(flag_id).empty();
        var ignored_words = [];
        var replaced_words = [];
        var offset_words = [];
        if (ret.data.ignored_words) {
          ignored_words = ret.data.ignored_words;
        }
        if (ret.data.replaced_words) {
          replaced_words = ret.data.replaced_words;
        }
        if (ret.data.offset_words) {
          offset_words = ret.data.offset_words;
        }
        var words = ignored_words + replaced_words + offset_words;
        if (words.length != 0) {
          if (ret.data.org_string) {
            $(info_id).append(`<div class="mb-3" ><label class="form-label">识别使用名称：<span class="badge badge-outline text-cyan me-1 mb-1" style="vertical-align: middle;white-space: normal;text-align: left;word-break: break-all;" title="自定义识别词过滤后识别用名">${ret.data.org_string}</span></label></div>`);
          }
          if (ignored_words.length != 0) {
            var ignored_word_content = '<div class="mb-3"><label class="form-label">应用屏蔽词：';
            for (ignored_word of ignored_words) {
              ignored_word_content = ignored_word_content + `<span class="badge badge-outline text-cyan me-1 mb-1" style="vertical-align: middle;">${ignored_word}</span>`;
            };
            $(info_id).append(ignored_word_content + '</label></div>');
          }
          if (replaced_words.length != 0) {
            var replaced_words_content = '<div class="mb-3"><label class="form-label">应用替换词：';
            for (replaced_word of replaced_words) {
              replaced_words_content = replaced_words_content + `<span class="badge badge-outline text-cyan me-1 mb-1" style="vertical-align: middle;">${replaced_word}</span>`;
            };
            $(info_id).append(replaced_words_content + '</label></div>');
          }
          if (offset_words.length != 0) {
            var offset_words_content = '<div class="mb-3"><label class="form-label">应用集数偏移：';
            for (offset_word of offset_words) {
              offset_words_content = offset_words_content + `<span class="badge badge-outline text-cyan me-1 mb-1" style="vertical-align: middle;">${offset_word}</span>`;
            };
            $(info_id).append(offset_words_content + '</label></div>');
          }
        }
        if (ret.data.type) {
          $(info_id).append(`<span class="badge badge-outline text-blue me-1 mb-1" title="类型">${ret.data.type}</span>`);
        }
        if (ret.data.category) {
          $(info_id).append(`<span class="badge badge-outline text-blue me-1 mb-1" title="类别">${ret.data.category}</span>`);
        }
        if (ret.data.name) {
          var name_query_link = `https://www.themoviedb.org/search?query=${encodeURI(ret.data.name)}`;
          $(info_id).append(`<span title="识别名称"><a class="badge badge-outline text-red me-1 mb-1" href="${name_query_link}" target="_blank">${ret.data.name}</a></span>`);
        }
        if (ret.data.title) {
          $(info_id).append(`<span class="badge badge-outline text-red me-1 mb-1" title="标题">${ret.data.title}</span>`);
        }
        if (ret.data.year) {
          $(info_id).append(`<span class="badge badge-outline text-orange me-1 mb-1" title="年份">${ret.data.year}</span>`);
        }
        if (ret.data.season_episode) {
          $(info_id).append(`<span title="季集"><a class="badge badge-outline text-orange me-1 mb-1" href="${ret.data.tmdb_S_E_link}" target="_blank">${ret.data.season_episode}</a></span>`);
        }
        if (ret.data.part) {
          $(info_id).append(`<span class="badge badge-outline text-orange me-1 mb-1" title="分集">${ret.data.part}</span>`);
        }
        if (ret.data.tmdbid) {
          $(info_id).append(`<span title="TMDB ID"><a class="badge badge-outline text-green me-1 mb-1" href="${ret.data.tmdblink}" target="_blank">${ret.data.tmdbid}</a></span>`);
        }
        if (ret.data.restype) {
          $(info_id).append(`<span class="badge me-1 mb-1" title="质量">${ret.data.restype}</span>`);
        }
        if (ret.data.pix) {
          $(info_id).append(`<span class="badge me-1 mb-1" title="分辨率">${ret.data.pix}</span>`);
        }
        if (ret.data.video_codec) {
          $(info_id).append(`<span class="badge me-1 mb-1" title="视频编码">${ret.data.video_codec}</span>`);
        }
        if (ret.data.audio_codec) {
          $(info_id).append(`<span class="badge me-1 mb-1" title="音频编码">${ret.data.audio_codec}</span>`);
        }
        if (ret.data.team) {
          $(info_id).append(`<span class="badge badge-outline text-yellow me-1 mb-1" title="制作组/字幕组">${ret.data.team}</span>`);
        }
        if (ret.data.match_flag) {
          $(flag_id).append(`<br><span class="badge badge-outline text-green me-1 mb-1">匹配</span>`);
        } else {
          $(flag_id).append(`<br><span class="badge badge-outline text-red me-1 mb-1">不匹配</span>`);
        }
        if (ret.data.exist_flag) {
          $(flag_id).append(`<br><span class="badge badge-outline text-green me-1 mb-1">本地已存在</span>`);
        } else {
          $(flag_id).append(`<br><span class="badge badge-outline text-orange me-1 mb-1">本地不存在</span>`);
        }
      }
    })
  }

  // 订阅下载记录
  function show_rss_history_modal(id) {
    show_wait_process();
    ajax_post("list_rss_history", { "id": id }, function (ret) {
      hide_wait_process();
      if (ret.code == 0) {
        downloads = ret.data;
        var content_th = `<thead><tr>
                          <th>标题</th>
                          <th>下载器</th>
                          <th>下载时间</th>
                          </tr></thead>`;
        var content_td = '';
        for (var i = 0; i < downloads.length; i++) {
          var download = downloads[i];
          var title = '';
          var downloader = '';
          var date = '';
          if (download.title) {
            title = `<span>${download.title}</span>`
          };
          if (download.date) {
            date = download.date.split(" ");
            date = `<small>${date[0]}<br>${date[1]}</small>`;
          };
          if (download.downloader) {
            downloader = `<span class="badge me-1 mb-1" title="下载器">${download.downloader}</span>`;
          };
          var content_td = `${content_td}<tr><td>${title}</td>><td>${downloader}</td><td class="text-nowrap">${date}</td></tr>`;
          content_tb = `<tbody>${content_td}</tbody>`;
          content = `${content_th}${content_tb}`;
        };
      } else {
        var content = `<div class="empty"><p class="empty-title">${ret.msg}</p></div>`;
      };
      $("#table-rss-history").empty();
      $("#table-rss-history").append(content);
      $("#modal-rss-history").modal('show');
    });
  }

  // 应用订阅模板
  function apply_templated(id) {
    ajax_post("get_userrss_task", { id: id }, function (ret) {
      if (ret.code == 0) {
        $("#userrss_interval").val(ret.detail.interval);
        $("#userrss_include").val(ret.detail.include);
        $("#userrss_exclude").val(ret.detail.exclude);
        $("#userrss_filterrule").val(ret.detail.filter);
        $("#userrss_parser").val(ret.detail.parser);
        $("#userrss_state").val(ret.detail.state);
        show_userrss_uses(ret.detail.uses);
        $("#userrss_save_path").val(ret.detail.save_path);
        $("#userrss_download_setting").val(ret.detail.download_setting);
      }
    });
  }

  // 单选框事件
  $('input[type=radio][name=userrss_uses]').change(function () {
    var use = this.value;
    show_userrss_uses(use);
  });

  // 自定义订阅动作 D:下载 R:订阅 S:搜索
  function show_userrss_uses(use) {
    if (use == 'D') {
      $("#downloader_setting_div").show();
      $("#filter_div").show();
      $("#use_D").prop("checked", true);
    }
    else if (use == 'R') {
      $("#downloader_setting_div").hide();
      $("#filter_div").hide();
      $("#use_R").prop("checked", true);
    }
    else if (use == 'S') {
      $("#downloader_setting_div").hide();
      $("#filter_div").hide();
      $("#use_S").prop("checked", true);
    }
  }
</script>